using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BodySoundSimulator.CaseData;

namespace BodySoundSimulator.Recording
{
    public partial class NewCase : Form
    {
        #region Fields
        string caseID;
        string diseaseCondition;
        string soundName;
        string details;

        Dictionary<string, CaseInformation> existingCases;
        #endregion

        #region Prpoerties
        public string CaseID
        {
            get { return caseID; }
            set { caseID = value; }
        }

        public string DiseaseCondition
        {
            get { return diseaseCondition; }
            set { diseaseCondition = value; }
        }

        public string SoundName
        {
            get { return soundName; }
            set { soundName = value; }
        }

        public string Details
        {
            get { return details; }
            set { details = value; }
        }
        #endregion

        #region Constructors
        public NewCase(Dictionary<string, CaseInformation> existingCases)
        {
            InitializeComponent();
            this.existingCases = existingCases;
        }
        #endregion

        #region UI event handlers
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!InvalidInput())
            {
                this.caseID = this.txtCaseID.Text;
                this.details = this.txtDetails.Text;
                this.diseaseCondition = this.txtDiseaseName.Text;
                this.soundName = this.txtSoundName.Text;

                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void TextBoxTextChanged(object sender, EventArgs e)
        {
            this.errTextBoxes.Clear();
        }
        #endregion

        #region Private methods
        private bool InvalidInput()
        {
            bool inError = false;
            if (string.IsNullOrEmpty(this.txtCaseID.Text))
            {
                inError = true;
                this.errTextBoxes.SetError(this.txtCaseID, "You must provide valid case ID");
            }
            else
            {
                if (existingCases.ContainsKey(this.txtCaseID.Text))
                {
                    inError = true;
                    this.errTextBoxes.SetError(this.txtCaseID, "The caseID you provided already exists. Please provide another valid case ID.");
                }
            }
            if (string.IsNullOrEmpty(this.txtSoundName.Text))
            {
                inError = true;
                this.errTextBoxes.SetError(this.btnSoundNames, "You must provide valid value for sound name");
            }
            if (string.IsNullOrEmpty(this.txtDiseaseName.Text))
            {
                inError = true;
                this.errTextBoxes.SetError(this.btnDiseases, "You must provide valid value for disease");
            }
            return inError;
        }
        #endregion
    }
}